package entretenimiento.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import entretenimiento.excepcion.DAOExcepcion;
import entretenimiento.modelo.ResponsableFamilia;
import entretenimiento.util.ConexionBD;

public class AdminResponsableFamiliaDAO extends ResponsableFamiliaDAO {

	public Collection<ResponsableFamilia> buscarPorNombre(String nombre)
			throws DAOExcepcion {
		String query = "select CoResponsable, NoNombres, NoApePaterno, NoApeMaterno, Flsexo  from responsable_familia where NoNombres like ?";
		Collection<ResponsableFamilia> lista = new ArrayList<ResponsableFamilia>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombre + "%");
			rs = stmt.executeQuery();
			
			while (rs.next()) {
				ResponsableFamilia vo = new ResponsableFamilia();
				vo.setCodigores(rs.getInt("CoResponsable"));
				vo.setNombres(rs.getString("NoNombres"));
				vo.setApepaterno(rs.getString("NoApePaterno"));
				vo.setApematerno(rs.getString("NoApeMaterno"));
				vo.setSexo(rs.getInt("Flsexo"));
				
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}

	public Collection<ResponsableFamilia> listar() throws DAOExcepcion {
		Collection<ResponsableFamilia> c = new ArrayList<ResponsableFamilia>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select CoResponsable, NoNombres, NoApePaterno, NoApeMaterno, Flsexo  from responsable_familia order by NoNombres";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				ResponsableFamilia vo = new ResponsableFamilia();
				vo.setCodigores(rs.getInt("CoResponsable"));
				vo.setNombres(rs.getString("NoNombres"));
				vo.setApepaterno(rs.getString("NoApePaterno"));
				vo.setApematerno(rs.getString("NoApeMaterno"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	
	
	
}
